package maze

import "fmt"

//import "container/list"



func MazePathQueue(start, end Point) bool {
	queue := []Node{}
	startNode := Node{Point: start, FromIndex: -1}
	path := make([]Node, 0)
	queue = append(queue, startNode)
	findEnd := false
	for len(queue) > 0 {
		currNode := queue[0]
		mazeMap[currNode.Point.X][currNode.Point.Y] = 2
		queue = queue[1:] //出队
		path = append(path, currNode)
		if currNode.Point.Equals(end) {
			findEnd = true
			break
		}
		for _, dir := range Dirs {
			nextNode := Node{Point: dir(currNode.Point), FromIndex: len(path) - 1}
			if nextNode.Point.X >= 0 &&
				nextNode.Point.X < len(mazeMap) &&
				nextNode.Point.Y >= 0 &&
				nextNode.Point.Y < len(mazeMap[nextNode.Point.X]) &&
				mazeMap[nextNode.Point.X][nextNode.Point.Y] == 0 {
				queue = append(queue, nextNode)
			}
		}

	}
	if len(path) > 0 && findEnd {
		printPath(path)
		return true
	}

	return false
}
func printPath(path []Node) {

	realPath := []Point{}
	currNode := path[len(path)-1]
	realPath = append(realPath, currNode.Point)
	for {
		if currNode.FromIndex >= 0 {
			currNode = path[currNode.FromIndex]
			realPath = append(realPath, currNode.Point)
		} else {
			break
		}
	}
	for i := len(realPath) - 1; i >= 0; i-- {
		fmt.Print(realPath[i])
	}
}
